using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Systems.CMS;
using Deepcode.Flux.Core.Systems.CMS.TemplateGallery;
using Deepcode.Flux.Core.Systems.Workflow.Bases;

public partial class Private_CMS_main_actions_checkin_checkinTaskpad : Deepcode.Flux.Core.UI.CMS.CMSActionPane
{
	#region Extra Viewstates
	/// <summary>
	/// Get/Set the CSV list of ID's
	/// </summary>
	protected string MultiIDs
	{
		get
		{
			object o = ViewState["MIDS"];
			if( o == null ) return "";
			return (string) o;
		}
		set
		{
			ViewState["MIDS"] = value;
		}
	}
	/// <summary>
	/// Get the MutliIDs CSV as an array of ints
	/// </summary>
	protected int[] iMultiIDs
	{
		get
		{
			List<int> list = new List<int>();
			string mids = MultiIDs;
			if( mids == "" ) return list.ToArray();

			string [] items = mids.Split(',');
			foreach (string si in items)
			{
				try
				{
					int i = Int32.Parse(si);
					list.Add(i);
				}
				catch { }
			}
			return list.ToArray();
		}
	}
	#endregion
	#region Page Load Event
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			MultiIDs = GetAttribute("MultiIDs", "");
		}
	}
	#endregion
	#region Interactions
	/// <summary>
	/// Type selected, go forward to the actual editor form itself
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnNext_Click(object sender, EventArgs e)
	{
		if( ! Page.IsValid ) return;

		// If there are no workflow definitions, make sure the checkin will publish
		Definition[] defs = new Definitions().GetDefinitionsByHandlerID("ContentManagement");
		bool bSubmitNow = (defs.Length<1);

		if (MultiIDs != "")
		{
			foreach (int i in iMultiIDs)
				DoCheckin( i, bSubmitNow );
		}
		else
			DoCheckin(this.ContentID, bSubmitNow);

		GoBack();
	}
	/// <summary>
	/// Do the checkin process for one item
	/// </summary>
	/// <param name="ContentID"></param>
	protected void DoCheckin( int contentID, bool bSubmitNow )
	{
		ContentManager cms = new ContentManager();
		ContentObject obj = cms.AdxGetByID( contentID );
		if (obj != null)
		{
			// Check this content in
			cms.CheckIn(contentID, CurrentUser.PK_ID, fNotes.Text);

			// Audit
			Deepcode.Flux.Core.Systems.Audit.Quick.QuickAudit.RaiseInformation(new Deepcode.Flux.Core.Systems.Audit.Quick.AuditUser(),
				"Flux",
				"Content Manager",
				"checked in {0}", obj.NodeTitle);


			// If there are no workflow definitions, just publish it now
			if (bSubmitNow)
			{
				cms.SubmitForApproval(contentID, -1, CurrentUser.PK_ID);
				// Audit
				Deepcode.Flux.Core.Systems.Audit.Quick.QuickAudit.RaiseInformation(new Deepcode.Flux.Core.Systems.Audit.Quick.AuditUser(),
					"Flux",
					"Content Manager",
					"auto submitted for approval - {0}", obj.NodeTitle);

			}
		}
	}
	/// <summary>
	/// Cancel clicked - go back to content view listing
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnCancel_Click(object sender, EventArgs e)
	{
		GoBack();
	}
	#endregion
	#region Utility
	/// <summary>
	/// Override go back to return to taskpad
	/// </summary>
	protected override void GoBack()
	{
		Response.Redirect("~/admin/menu/taskpad.aspx", true);
	}
	#endregion
}
